package com.preloader
{
	import flash.preloader.AbstractPreloader;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import com.myproject.preloader.CustomPreloaderUI;
	
	/**
	 * An example implementation of AbstractPreloader to create a custom preloader.
	 * 
	 * @author Omar Gonzalez
	 * 
	 */	
	public class CustomPreloader extends AbstractPreloader
	{
		/**
		 * Reference to the instance of the preloader MovieClip compiled
		 * with CustomPreloaderUI.
		 */		
		private var _customPreloaderUI:CustomPreloaderUI;
		
		/**
		 * @Constructor
		 * 
		 */		
		public function CustomPreloader()
		{
			// Set full qualified class name of main project.
			// This is usually just a name as below because the
			// AS3 application files are usually in the src root folder.
			mainClass = "DesignoutputCard";
			
			super();
			
			_createCustomDisplay();
		}
		/**
		 * Creates an instance of the MovieClip compiled in CustomPreloaderUI.fla
		 * as com.myproject.preloader.CustomPreloaderUI
		 * 
		 */		
		private function _createCustomDisplay():void
		{
			// Here you can start the view of the custom preloader thats gonna get animated.
			_customPreloaderUI = new CustomPreloaderUI();
			
			// Set the progress bar to 0
			_customPreloaderUI.ui_mc_progressBar.scaleX = 0;
			
			// Add custom preloader MovieClip to the stage
			addChild( _customPreloaderUI );
			
			// Center the preloader
			_customPreloaderUI.x = stage.stageWidth * .55 - _customPreloaderUI.width * .5;
			_customPreloaderUI.y = stage.stageHeight * .55 - _customPreloaderUI.height * .5;
		}
		/**
		 * A function that gets FlashVars, placed as an example of what might be
		 * done in the onInitLoading() override.
		 * 
		 */		
		private function _getFlashVars():void
		{
			// Would get some Flash Vars here needed for the preloading animation.
		}
		/**
		 * Override of AbstractPreloader.onInitLoading(), here you can initialize
		 * any fields that might be needed during progress download, such as 
		 * getting flash vars. 
		 * 
		 * @param event
		 * 
		 */		
		override protected function onInitLoading(event:Event):void
		{
			super.onInitLoading(event);
			
			// Add logic to do things before preloading begins. 
			// Example:  Get FlashVars needed during preloading animation.
			_getFlashVars();
		}
		/**
		 * Override of the AbstractPreloader.onProgress() method to display
		 * download progress feedback.
		 * 
		 * @param event
		 * 
		 */		
		override protected function onProgress(event:ProgressEvent):void
		{
			// Here you can update the progress using the view created in _createCustomDisplay()
			var percentage:Number = event.bytesLoaded / event.bytesTotal;
			_customPreloaderUI.ui_mc_progressBar.scaleX = percentage;
			
			var label:String = Math.floor( (percentage * 100) ).toString() + "% Loaded";
			_customPreloaderUI.ui_tf_label.text = label;
		}
		/**
		 * Override of onComplete() to set the scale of the bar to full scale.
		 * 
		 * @param event
		 * 
		 */		
		override protected function onComplete(event:Event):void
		{
			_customPreloaderUI.ui_mc_progressBar.scaleX = 1;
			
			super.onComplete(event);
		}
	}
}